package com.guigu.serviceorder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.common_util.pojo.IdWorker.IdWorker;
import com.guigu.common_util.pojo.utils.Result;
import com.guigu.serviceorder.entity.*;
import com.guigu.serviceorder.mapper.TbOrderItemMapper;
import com.guigu.serviceorder.mapper.TbReturnOrderItemMapper;
import com.guigu.serviceorder.mapper.TbReturnOrderMapper;
import com.guigu.serviceorder.service.ITbReturnOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2020-07-08
 */
@Service
public class TbReturnOrderServiceImpl extends ServiceImpl<TbReturnOrderMapper, TbReturnOrder> implements ITbReturnOrderService {

    @Autowired
    private TbReturnOrderItemMapper tbReturnOrderItemMapper;

    @Autowired
    private TbReturnOrderMapper tbReturnOrderMapper;

    @Autowired
    private TbOrderItemMapper tbOrderItemMapper;



    @Override
    public Result findPage1(Map<String, Object> searchMap, Integer page, Integer size) {
        QueryWrapper<TbReturnOrder> wrapper = createExample(searchMap);
        wrapper.eq("type",1);
        //创建page对象
        Page<TbReturnOrder> pageTbReturnOrder = new Page<>(page,size);
        baseMapper.selectPage(pageTbReturnOrder,wrapper);
        if (pageTbReturnOrder.getRecords() == null && pageTbReturnOrder.getTotal() == 0){
            return Result.error();
        }
        return Result.ok().data("rows",pageTbReturnOrder.getRecords()).data("total",pageTbReturnOrder.getTotal());
    }

    @Override
    public Result findPage2(Map<String, Object> searchMap, Integer page, Integer size) {
        QueryWrapper<TbReturnOrder> wrapper = createExample(searchMap);
        wrapper.eq("type",2);
        //创建page对象
        Page<TbReturnOrder> pageTbReturnOrder = new Page<>(page,size);
        baseMapper.selectPage(pageTbReturnOrder,wrapper);
        if (pageTbReturnOrder.getRecords() == null && pageTbReturnOrder.getTotal() == 0){
            return Result.error();
        }
        return Result.ok().data("rows",pageTbReturnOrder.getRecords()).data("total",pageTbReturnOrder.getTotal());
    }

    @Override
    public Result agreeRefund(String id, Integer money, Integer adminId) {
        TbReturnOrder returnOrder = tbReturnOrderMapper.selectById(id);
        if(returnOrder==null){
            throw new RuntimeException("退款订单不存在！");
        }
        if(!returnOrder.getType().equals("2")){
            throw new RuntimeException("不是退款订单！");
        }
        if(money>returnOrder.getReturnMoney() || money<=0){
            throw new RuntimeException("退款金额不合法！");
        }
        returnOrder.setReturnMoney(money);
        returnOrder.setStatus("1");//同意
        returnOrder.setAdminId(adminId);//管理员
        returnOrder.setDisposeTime(new Date());//处理日期
        int num =tbReturnOrderMapper.updateById(returnOrder);//保存
        if(num>0)
            return Result.ok();
        return Result.error();
// 调用支付平台的退款接口
    }

    @Override
    public Result rejectRefund(String id, String remark, Integer adminId) {
        TbReturnOrder returnOrder = tbReturnOrderMapper.selectById(id);
        if (returnOrder == null) {
            throw new RuntimeException("退款订单不存在！");
        }
        if (!returnOrder.getType().equals("2")) {
            throw new RuntimeException("不是退款订单！");
        }
        if (remark.length() < 5) {
            throw new RuntimeException("请输入驳回理由！");
        }
        //修改属性
        returnOrder.setRemark(remark);//驳回理由
        returnOrder.setStatus("2");//驳回
        returnOrder.setAdminId(adminId);//管理员
        returnOrder.setDisposeTime(new Date());//处理日期
        tbReturnOrderMapper.updateById(returnOrder);//保存
        //修改对应订单明细的退款状态为未申请
        QueryWrapper<TbReturnOrderItem> wrapper = new QueryWrapper<>();
        wrapper.eq("return_order_id",id);
        List<TbReturnOrderItem> tbReturnOrderItems = tbReturnOrderItemMapper.selectList(wrapper);
        int num=0;
        for (TbReturnOrderItem tbReturnOrderItem : tbReturnOrderItems) {
            TbOrderItem tbOrderItem = new TbOrderItem();
            String orderId = tbReturnOrderItem.getOrderId();
            tbOrderItem.setId(tbReturnOrderItem.getOrderId().toString());//提取订单明细
            tbOrderItem.setIsReturn("0");
            num +=tbOrderItemMapper.updateById(tbOrderItem);
        }
        if(num>0)
            return Result.ok();
        return Result.error();
    }

    @Override
    public Result agreeRefundGoods(String id, Integer adminId) {
        TbReturnOrder returnOrder = tbReturnOrderMapper.selectById(id);
        if(returnOrder==null){
            throw new RuntimeException("退货订单不存在！");
        }
        if(!returnOrder.getType().equals("1")){
            throw new RuntimeException("不是退货订单！");
        }
        returnOrder.setStatus("1");//同意
        returnOrder.setAdminId(adminId);//管理员
        returnOrder.setDisposeTime(new Date());//处理日期
        int num= tbReturnOrderMapper.updateById(returnOrder);//保存
        if(num>0)
            return Result.ok();
        return Result.error();
    }
    @Override
    public Result rejectRefundGoods(String id, String remark, Integer adminId) {
        TbReturnOrder returnOrder = tbReturnOrderMapper.selectById(id);
        if (returnOrder == null) {
            throw new RuntimeException("退货订单不存在！");
        }
        if (!returnOrder.getType().equals("1")) {
            throw new RuntimeException("不是退货订单！");
        }
        if (remark.length() < 5) {
            throw new RuntimeException("请输入驳回理由！");
        }
        //修改属性
        returnOrder.setRemark(remark);//驳回理由
        returnOrder.setStatus("2");//驳回
        returnOrder.setAdminId(adminId);//管理员
        returnOrder.setDisposeTime(new Date());//处理日期
        tbReturnOrderMapper.updateById(returnOrder);//保存
        //修改对应订单明细的退货状态为未申请
        QueryWrapper<TbReturnOrderItem> wrapper = new QueryWrapper<>();
        wrapper.eq("return_order_id",id);
        List<TbReturnOrderItem> tbReturnOrderItems = tbReturnOrderItemMapper.selectList(wrapper);
        int num =0;
        for (TbReturnOrderItem tbReturnOrderItem : tbReturnOrderItems) {
            TbOrderItem tbOrderItem = new TbOrderItem();
            String orderId = tbReturnOrderItem.getOrderId();
            tbOrderItem.setId(tbReturnOrderItem.getOrderId().toString());//提取订单明细
            tbOrderItem.setIsReturn("0");
            num += tbOrderItemMapper.updateById(tbOrderItem);
        }
        if(num>0)
            return Result.ok();
        return Result.error();
    }

    @Override
    public Result findOrderItemByOrderId(String orderId) {
        QueryWrapper<TbOrderItem> wrapper =new QueryWrapper<>();
        wrapper.eq("order_id",orderId);
        List<TbOrderItem> list = this.tbOrderItemMapper.selectList(wrapper);
        if (list!=null){
            return Result.ok().data("list",list);
        }
        return Result.error();
    }
    @Override
    public Result findReturnOrderItemByReturnOrderId(String returnOrderId) {
        QueryWrapper<TbReturnOrderItem> wrapper =new QueryWrapper<>();
        wrapper.eq("return_order_id",returnOrderId);
        List<TbReturnOrderItem> list = this.tbReturnOrderItemMapper.selectList(wrapper);
        if (list!=null){
            return Result.ok().data("list",list);
        }
        return Result.error();
    }
    @Override
    public Result deletereturnOrder(List<TbReturnOrder> returnorders) {
        int row =0;
        for (TbReturnOrder returnorder : returnorders) {
            System.out.println(returnorder);
            row+=this.tbReturnOrderMapper.deleteById(returnorder);
        }
        if(row>0)
            return Result.ok();
        return Result.error();
    }

    // 根据查询条件封装模块对象
    private QueryWrapper<TbReturnOrder> createExample(Map<String, Object> searchMap) {
        QueryWrapper<TbReturnOrder> wrapper = new QueryWrapper<>();
        if(searchMap!=null) {
            if(searchMap.get("id")!=null)
                wrapper.eq("id",searchMap.get("id"));
            if(searchMap.get("orderId")!=null && !"".equals(searchMap.get("orderId")))
                wrapper.like("orderId",searchMap.get("orderId"));
        }
        return wrapper;
    }
}
